package com.crm.dao.permission;

import com.crm.pojo.Userinfo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface UserInfoDAO extends CrudRepository<Userinfo, Long> {


	@Query("from Userinfo ui where ui.ustatus='是' order by ui.id desc")
	public List<Userinfo> queryAllPage(Pageable pageable);
	
	@Query("from Userinfo ui where ui.uname like ?1 and ui.ustatus='是' order by ui.id desc ")
	public List<Userinfo> queryNameUser(String uname,Pageable pageable);
	
	@Query("from Userinfo ui where ui.ustatus='是' order by ui.id desc ")
	public List<Userinfo> queryAllUser();
	
	@Query(nativeQuery = true,value="select * from userinfo u JOIN CRM_DEPT c on "
			+ " u.CRM_DEPTNO = c.CRM_DEPTNO  where c.crm_deptno=?1 and u.ustatus='是' ")
	public List<Userinfo> queryDeptUser(Long crmDeptno);
	
	@Query(value="select count(*) from Userinfo ui where ui.ustatus='是' ")
	public Long queryUserCount();
	
	@Query(value="select count(*) from Userinfo ui where ui.ustatus='是' and ui.uname like ?1 ")
	public Long queryNameUserCount(String uname);
	
	@Query(" from Userinfo ui where ui.ustatus='是' and ui.uname = ?1 ")
	public Userinfo queryNameUser(String uname);
	
	@Query(" from Userinfo ui where ui.ustatus='是' and ui.id = ?1 ")
	public Userinfo queryIdUser(Long uId);
	
	@Query(" from Userinfo ui "
			+ " where ui.ustatus='是' and ui.uname = ?1 and ui.upassword=?2 ")
	public Userinfo findByUnameAndUpassword(String uname,String upassword);
	
	@Query(" select m.id from Userinfo u inner join u.roles r "
			+ " inner join r.menubars m where u.id = ?1 ")
	public List<Long> queryMidsByuId(Long uId);

	@Query(" select uname from Userinfo ui where ui.ustatus='是' ")
	public List<String> queryNameUpdate();
	
	@Query(nativeQuery = true,value = "select  *  from USERINFO  u where u.ustatus='是' and(u.UNAME like ?1 or u.UREAL_NAME like ?1)")
	public List<Userinfo> findAllUsersBymh(String q);
	

}
